//
//  HD_LiDa_PlayerTool.m
//  HDMusicDemo
//
//  Created by Lida on 2017/8/14.
//  Copyright © 2017年 Lida. All rights reserved.
//

#import "HD_LiDa_PlayerTool.h"
#import "HD_LiDa_Playermanager.h"
#import <MediaPlayer/MediaPlayer.h>
@implementation HD_LiDa_PlayerTool

singleton_implementation(HD_LiDa_PlayerTool)

- (void)prepareForPlayWithMusic:(MusicInfo *)music {
    _musicInfo = music;
#warning ---Musicpath  根据具体情况修改
    NSURL *musicUrl = [[NSBundle mainBundle] URLForResource:music.musicPath withExtension:nil];
    AVPlayerItem *playItem = [[AVPlayerItem alloc]initWithURL:musicUrl];
    self.player = [AVPlayer playerWithPlayerItem:playItem];
    
    [[NSNotificationCenter defaultCenter] postNotificationName:ShouldPrepareToPlay object:nil];
    [self setLockInfo];
    [self addTimeObserverToPlayer];
}

- (void)setLockInfo{
#warning -----MusicInfo
    /*以下信息根据具体的音乐信息进行适当修改*/
    //设置锁屏音乐信息
    NSMutableDictionary *info = [NSMutableDictionary dictionary];
    //设置专辑名称
    info[MPMediaItemPropertyAlbumTitle] = @"网络热曲";
    //设置歌曲名
    info[MPMediaItemPropertyTitle] = _musicInfo.musicName;
    //设置歌手
    info[MPMediaItemPropertyArtist] = _musicInfo.songer;
    //设置专辑图片
    MPMediaItemArtwork *artwork = [[MPMediaItemArtwork alloc] initWithImage:[UIImage imageNamed:_musicInfo.icon]];
    info[MPMediaItemPropertyArtwork] = artwork;
    //设置歌曲时间
    //    double duration = CMTimeGetSeconds([self.player.currentItem duration]);
    //    info[MPMediaItemPropertyPlaybackDuration] = @(duration);
    [MPNowPlayingInfoCenter defaultCenter].nowPlayingInfo = info;
}

- (void)addTimeObserverToPlayer {
    if (self.timeObserver)
        return;
    
    double interval = 0.1;
    
    __block __weak HD_LiDa_PlayerTool *player = self;
    self.timeObserver = [self.player addPeriodicTimeObserverForInterval:CMTimeMakeWithSeconds(interval, NSEC_PER_SEC)
                                                                  queue:dispatch_get_main_queue()
                                                             usingBlock:^(CMTime time){
                                                                 [player updateTimeValues];
                                                             }];
}

- (void)removeTimeObserverFromPlayer
{
    if (self.timeObserver)
    {
        [self.player removeTimeObserver:self.timeObserver];
        self.timeObserver = nil;
    }
}


- (void)updateTimeValues {
    
    double duration = CMTimeGetSeconds([self.player.currentItem duration]);
    self.totalTime = duration;
    double seconds = CMTimeGetSeconds([self.player currentTime]);
    _currentTime = seconds;
}

- (void)play
{
    _isPlaying = YES;
    [self.player play];
    [[NSNotificationCenter defaultCenter] postNotificationName:ShouldPlay object:nil];
}

- (void)pause
{
    _isPlaying = NO;
    [self.player pause];
    [[NSNotificationCenter defaultCenter] postNotificationName:ShouldPause object:nil];
}

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
    [self prepareForPlayWithMusic:[[HD_LiDa_Playermanager sharedHD_LiDa_Playermanager] nextMusicInfo]];
    [self play];
}

- (void)setCurrentTime:(float)currentTime{
    _currentTime = currentTime;
    double duration = CMTimeGetSeconds([self.player.currentItem duration]);
    double seconds = CMTimeGetSeconds([self.player currentTime]);
    if (isfinite(seconds)) {
        if (seconds < 0.0) {
            seconds = 0.0;
        }
        
    }
    if (isfinite(duration)) {
        double time = _currentTime;
        [self.player seekToTime:CMTimeMakeWithSeconds(time, NSEC_PER_SEC)];
        
    }
}

- (void)stopAudio{
    [self.player pause];
    [self removeTimeObserverFromPlayer];
    _isPlaying = NO;
    
}



@end
